<template>
  <el-container class="home-container">
    <!--  头部区域  -->
    <el-header>
      <div>
        <img src="../assets/img/login.jpeg" class="img_home_logo" alt="zyyy">
        <span>ANDY</span>
      </div>
      <el-button type="success" @click="logout" round>退出</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside class="el_aside" :width="isCollapsed ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域-->
        <el-menu
          background-color="#001529"
          text-color="#fff"
          :unique-opened="true"
          :collapse="isCollapsed"
          :collapse-transition="false"
          router
          :default-active="activePath"
          active-text-color="#fff">
          <!-- 一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区-->
            <template slot="title">
              <!-- 图标-->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本-->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item class="el-menu-item" :index="'/' + subItem.path"
                          @click="saveNavState('/' + subItem.path)" :key="subItem.id" v-for="subItem in item.children">
              <template slot="title">
                <!-- 图标-->
<!--                <i class="el-icon-location"></i>-->
                <!-- 文本-->
                <span>{{subItem.authName}}</span>
              </template></el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!--右侧内同主体区域-->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
import axios from 'axios';

export default {
  name: 'Home',
  created () {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  data () {
    return {
      //  侧边栏主菜单
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapsed: false,
      // 被激活的连接
      activePath: ''
    };
  },

  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    //  获取所有菜单
    async getMenuList () {
      const { data: res } = await axios.get('menus');
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
    // 点击按钮，切换菜单的折叠和展开
    toggleCollapse () {
      this.isCollapsed = !this.isCollapsed;
    },
    // 保存选中的列表菜单
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    }
  }

};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  display: flex;
  // 左右布局
  justify-content: space-between;
  background-color: #001529;
  // 控件居中
  align-items: center;
  color: #fff;
  font-size: 25px;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 40px;
    }
    span {
      margin-left: 15px;
    }
  }
}

.el_aside {
  background-color: #001529;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #f0f2f5;
}

.img_home_logo {
  border-radius: 50%;
}
// vue---el-menu-item更改导航菜单栏选中的背景颜色
.el-menu-item.is-active {
  background-color: #3370ff !important;
  color: #fff;
  span {
    color: #fff !important;
  }
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px ;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}

</style>
